/*
* write_route.c
* Sunseker modelling (solar car preformance modelling) project
* This function writes a route structure into a route file.
* A prototype route.dat file:
*/
//  # File: route_sample.dat 
//  # This is a sample route file for Sunseeker modeling
//  #
//  # A '#' indicates that the rest of a line is a comment
//  #
//  # Each line that begins with a variable name holds the
//  # settings for that variable.
//  #
//  # The order of the variables can not be changed and no 
//  # variables can be ommitted.
//  #
//  # The version number si used to insure that a coute.dat file
//  # us read by the matchiung input routine. As this file format
//  # may change over time.
//  #
//  # If the route has n legs, there are n+1 data lines
//  version "0.5"                     # The version of this file
//  comment "Kalamazoo to GR route a" # A comment about the route
//  units   "metric"                  # "metric" or "english"
//  nleg    4                         # number of legs in the route
//  #
//  # Iam, "can" we compute distance, d_x, d_y, and d_x from lat/long?
//  # and do we want to? There is some advantage in the redundency.
//  # We need to be able to generate this type of file with reasonable
//  # consistancy between lat/long, distance, altitude, and direction.
//  #
//  # leg         end of leg (0 is the start or the first leg)
//  #             "metric" "english"
//  # distance     KM       Mi    , of f leg or perhaps better of the route so far.
//  # latitude
//  # longitude            
//  #             "metric" "english"
//  # altitude     M         Ft   , for E_potential
//  # speed_limit  KM/H      Mi/H , do we also need a speed min?
//  # stops                       , stop factor, for now 1.0 means a stop at the end of leg
//  # hills                       , factor for hilly terrain
//  # shade                       , for P_array
//  # d_x d_y d_z direction of leg, local coordinates, for D_a with a wind.
//  #
//  # leg distance latitude  longitude  altitude speed_limit stops hills d_x   d_y   d_z    Suntime
//  # --- -----    --------- ---------- -------- ----------- ----- ----- ----- ----- -----  ------
//    0   60.0     12231.000 23341.1231 1000.0   80.0        0.0   0.0   1.0   2.0   1.0    0
//    1   72.0     12231.400 23341.0001 1090.0   50.0        1.0   0.0   1.0   2.0   1.0    0
//    2   21.0     12231.600 23341.4500 1002.0   70.0        0.0   0.0   1.0   2.0   1.0    3600
//    3   93.0     12231.900 23344.9851 1302.0   80.0        0.0   0.0   1.0   2.0   1.0    0
//    4   10.0     12234.000 23341.3241 1502.0   80.0        0.0   0.0   1.0   2.0   1.0    0
#include <stdio.h>
#include <stdlib.h>
#include "ssm.h"

#define VERSION "0.5"

// Note that no sanity checking is being done in write_route(), perhaps it should be  ...
int write_route(const char *file, const Route *route) 
{
	FILE *fp;
	int   i;
	//
	//
	 #ifdef DEBUG
		fprintf(stderr,"DEBUG: entering write_route(), file=\"%s\"\n", file);
	 #endif
	if((fp=fopen(file,"w")) == NULL) 
	{
		fprintf(stderr,"ERROR: can not open route file %s for output\n", file);
		exit(1);
	} 
	// Not checking the return value is ok, since on EOF the next call will be EOF too
	fprintf(fp,"version %s\n", route->version);
	fprintf(fp,"comment %s\n", route->comment);
	fprintf(fp,"units %s\n", route->units);
	fprintf(fp,"nleg %d\n", route->nleg);

	for(i=0; i<=route->nleg; i++)  
	{
		
		//  # leg distance latitude longitude altitude speed_limit stops hills d_x d_y d_z Suntime
		fprintf(fp,"%d %f %f %f %f %f %f %f %f %f %f %f \n",
       			route->leg[i].i,
       			route->leg[i].distance,
       			route->leg[i].latitude,
       			route->leg[i].longitude,
       			route->leg[i].altitude,
       			route->leg[i].speed_limit,
       			route->leg[i].stops,
       			route->leg[i].hills,
       			route->leg[i].d_x, route->leg[i].d_y, route->leg[i].d_z,
			route->leg[i].suntime);
	}
	return (0);
}
